package com.hsd;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

public class MybatisUtil {
    private static SqlSessionFactory sqlSessionFactory;


    public static SqlSessionFactory getSqlSessionFactory() throws IOException {
        if (sqlSessionFactory==null){
            Reader reader = Resources.getResourceAsReader("spring.xml");
            //读取mybaties的xml配置信息，并创建SqlSessionFactory对象
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        }
        return sqlSessionFactory;
    }

    public static SqlSession getSqlSession() throws IOException {
        //通过SqlSessionFactory对象创建SqlSession对象（通过SqlSession对象实现数据库访问）
        return getSqlSessionFactory().openSession();
    }
}
